﻿using System;
using System.Collections;
using System.Data;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for ContactUserDB
/// </summary>
/// 
namespace CE.User
{
    public class ContactUserDB
    {
        public ContactUserDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(ContactUser contactuser)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocContactUserDetailInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@RefCEUserId", contactuser.RefCEUserId);
            _sPara.Add("@ProductId", contactuser.ProductId);
            _sPara.Add("@ProviderCompanyId", contactuser.ProviderCompanyId);
            _sPara.Add("@InsuranceType", contactuser.InsuranceType);
            _sPara.Add("@FullName", contactuser.FullName);
            _sPara.Add("@ContactNo", contactuser.ContactNo);
            _sPara.Add("@CallTime", contactuser.CallTime);
            _sPara.Add("@Status", contactuser.Status);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public Int64 Update(Int64 CEID, Int64 AgentId, Int64 ProviderCompanyId, string ProductType, Int64 ProductId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocContactUserDetailUpdateAgentIdSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CEID", CEID);
            _sPara.Add("@AgentId", AgentId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            _sPara.Add("@ProductType", ProductType);
            _sPara.Add("@ProductId", ProductId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public ContactUser GetCallMe(Int64 RefCEUserID, Int64 ProductId, string InsType, Int64 ProviderCompanyId)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocContactUserSelectSingleItem";
            ContactUser _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@RefCEUserId", RefCEUserID);
            _sPara.Add("@ProductId", ProductId);
            _sPara.Add("@InsuranceType", InsType);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new ContactUser();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        private ContactUser FillDataRecord(DataTable myDataTable)
        {
            ContactUser myContact = new ContactUser();
            myContact.CallMeId = Convert.ToInt64(myDataTable.Rows[0]["CallMeId"].ToString());
            myContact.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myContact.ProductId = Convert.ToInt64(myDataTable.Rows[0]["ProductId"].ToString());
            myContact.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[0]["ProviderCompanyId"].ToString());
            myContact.InsuranceType = myDataTable.Rows[0]["InsuranceType"].ToString();
            myContact.FullName = myDataTable.Rows[0]["FullName"].ToString();
            myContact.ContactNo = myDataTable.Rows[0]["ContactNo"].ToString();
            myContact.CallTime = Convert.ToInt16(myDataTable.Rows[0]["CallTime"].ToString());
            myContact.CreatedDate = Convert.ToDateTime(myDataTable.Rows[0]["CreatedDate"].ToString());
            myContact.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[0]["ModifiedDate"].ToString());
            return myContact;
        }
    }
}